package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.conmmon.CustomException;
import com.itheima.dao.DishDao;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavor;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.DishDto;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liwanhu
 * @datetime 2021/10/25 21:16
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 添加菜品
     * dishDto是根据Dish扩展来的实体类,因为浏览器返回得数据中有Dish表中没有的数据,所有需要扩展数据
     * @param dishDto
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品Dish信息
        this.save(dishDto);
        //获取菜品ID
        Long id = dishDto.getId();
        //获取浏览器返回过来的Dish_flavors口味表的信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历口味数组信息item表示要遍历的信息,代表了flavors
        flavors = flavors.stream().map((item)->{
            item.setDishId(id);
            return item;
        //collect(Collectors.toList()结束流,并将数据lambda表达式中的数据整合到一起
        }).collect(Collectors.toList());
        //批量保存菜品口味信息到表中
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询所对应的菜品已经菜品的口味
     * @param id
     * @return
     */
    @Override
    public DishDto getDishWithFlavor(Long id) {
        //查询出所有的菜品信息
        Dish byId = this.getById(id);
        //new一个空的DishDto对象
        DishDto dishDto = new DishDto();
        //将菜品信息拷贝到DishDto对象中
        BeanUtils.copyProperties(byId,dishDto);
        //判断口味中的菜品id是否与菜品id一致
        QueryWrapper<DishFlavor> qw = new QueryWrapper<>();
        qw.eq("dish_id",id);
        List<DishFlavor> list = dishFlavorService.list(qw);
        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //获取当前需要修改的对象
        this.updateById(dishDto);
        //创建条件构造器
        QueryWrapper<DishFlavor> qw = new QueryWrapper<>();
        qw.eq("dish_id",dishDto.getId());
        //根据id删除所对应菜品口味
        dishFlavorService.remove(qw);
        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            dishFlavorService.save(flavor);
        }
       /* flavors = flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());*/
        //dishFlavorService.saveBatch(flavors);
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void removeDish(List<Long> ids) {
        //创建条件构造器 查询本次要删除的套餐状态是否是启售状态,列名:status
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        //判断菜品ids在表中是否存在
        queryWrapper.in("id",ids);
        //如果是启售状态不能被删除,如果不是启售状态则可以删除
        queryWrapper.eq("status",1);
        int count = this.count(queryWrapper);
        if (count > 0){
            throw new CustomException("菜品在起售中,不可被删除");
        }
        QueryWrapper<DishFlavor> qw = new QueryWrapper<>();
        qw.in("dish_id",ids);
        this.removeByIds(ids);
    }
}
